package com.qf.web;

import com.qf.service.SmsCheckService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 徐老板
 * @date 2020/2/27   10:40
 * 这次试用原生的servlet进行书写
 */
//这个就相当于controller注解和requestmap注解的组合 只不过是原生的 不加入spring容器中的
@WebServlet(urlPatterns = "/smsinterface",name = "SmsServlet")
public class InterfaceServlet extends HttpServlet {
    private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceServlet.class);

    @Resource
    private SmsCheckService smsCheckService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
//        resp.setContentType("text/html;charset=utf-8");
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("UTF-8");
        String clientId = req.getParameter("clientId");

        LOGGER.error("InterfaceServlet中接收前端传过来的id是{}",clientId);

        String srcID = req.getParameter("srcID");
        String mobile = req.getParameter("mobile");
        String content = req.getParameter("content");
        String pwd = req.getParameter("pwd");//此处应该是一个原始密码
        String ip = getRealIp(req);//获取 ip

        smsCheckService.checkSms(clientId, pwd,srcID ,ip ,content , mobile);

    }


    public static String getRealIp(HttpServletRequest req){
        String ip = null;
        String xIP = req.getHeader("X-Real-IP");
        String xFor = req.getHeader("X-Forwarded-For");
        if (!StringUtils.isEmpty(xFor)&&!"unknow".equalsIgnoreCase(xFor)){
            //这个方法注意一下
            int index = xFor.indexOf(",");
            if (index>0){
                return xFor.substring(0,index);
            }
            return xFor;

        }

        ip=xIP;
        //要写下面的判断条件 这个地方要写一个if判断 这里没写

        if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
            ip = req.getHeader("Proxy-Client-IP");
        }
        //上面获取的有可能还是空的
        if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
            ip = req.getHeader("WL-Proxy-Client-IP");
        }

        if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
            ip = req.getHeader("HTTP_CLIENT_IP");
        }

        if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
            ip = req.getHeader("HTTP_X_FORWARDED_FOR");
        }

        if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
            ip = req.getRemoteAddr();
        }

        return ip;

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
